package jiang.Algorithm;

/**
 * @ClassName ArabicToChinese
 * @Description: 用chatGPT写的算法
 * @Author jiangshitao
 * @Date 2023/2/4
 *
 * 当数字的位数超过6位的时候就会出现bug
 */
public class ArabicToChinese {
    private static final String[] nums = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] units = {"", "十", "百", "千", "万", "亿"};

    public static String convert(int num) {
        if (num == 0) {
            return "零";
        }
        StringBuilder sb = new StringBuilder();
        int digit = 0;
        while (num > 0) {
            int n = num % 10;
            if (n != 0) {
                sb.insert(0, units[digit]);
                sb.insert(0, nums[n]);
            } else {
                if (sb.length() > 0 && sb.charAt(0) != '零') {
                    sb.insert(0, "零");
                }
            }
            digit++;
            num /= 10;
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        int num = 999999;
        System.out.println(ArabicToChinese.convert(num));
    }
}
